import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
    // 首页是公开页面，不需要认证
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue'),
    meta: { requiresAuth: true } // 添加路由守卫标记
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register-verify',
    name: 'register-verify',
    component: () => import('../views/RegisterVerify.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/RegisterForm.vue')
  },
  // 管理员路由
  {
    path: '/admin',
    name: 'admin',
    component: () => import('../views/admin/Index.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/users',
    name: 'admin-users',
    component: () => import('../views/admin/UserManagement.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/admin/user/edit/:id',
    name: 'admin-user-edit',
    component: () => import('../views/admin/UserEdit.vue'),
    meta: { requiresAuth: true }
  },
  // 教师路由
  {
    path: '/teacher',
    name: 'teacher',
    component: () => import('../views/teacher/Index.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher/student-grades',
    name: 'teacher-student-grades',
    component: () => import('../views/teacher/StudentGrades.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher/create-subject',
    name: 'teacher-create-subject',
    component: () => import('../views/teacher/CreateSubject.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher/subjects',
    name: 'teacher-subjects',
    component: () => import('../views/teacher/SubjectList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher/import-questions-manual',
    name: 'teacher-import-questions-manual',
    component: () => import('../views/teacher/ImportQuestionsManual.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher/import-questions-excel',
    name: 'teacher-import-questions-excel',
    component: () => import('../views/teacher/ImportQuestionsExcel.vue'),
    meta: { requiresAuth: true }
  },
  // 学生路由
  {
    path: '/student',
    name: 'student',
    component: () => import('../views/student/Index.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/student/teachers',
    name: 'student-teachers',
    component: () => import('../views/student/TeacherList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/student/exam-records',
    name: 'student-exam-records',
    component: () => import('../views/student/ExamRecords.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/student/no-exam-list',
    name: 'student-no-exam-list',
    component: () => import('../views/student/NoExamList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/student/take-exam/:subjectId',
    name: 'student-take-exam',
    component: () => import('../views/student/TakeExam.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 如果目标路由需要认证但没有token，则跳转到登录页
  if (to.meta.requiresAuth && !token) {
    next({ name: 'login' })
  } else {
    // 如果用户已认证且正在尝试访问登录页，则重定向到主页
    if (to.name === 'login' && token) {
      // 根据角色跳转到对应主页
      const userInfo = localStorage.getItem('userInfo')
      if (userInfo) {
        try {
          const user = JSON.parse(userInfo)
          switch (user.roleId) {
            case 1: // 管理员
              next('/admin')
              break
            case 2: // 教师
              next('/teacher')
              break
            case 3: // 学生
              next('/student')
              break
            default:
              next({ name: 'home' })
          }
        } catch (e) {
          next({ name: 'home' })
        }
      } else {
        next({ name: 'home' })
      }
    } else {
      next()
    }
  }
})

export default router